package web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import web.pojo.Student;
import web.pojo.User;
import web.service.StudentService;
import web.service.TeacherService;
import web.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    /**
     * 返回人员总数
     * 测试接口
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("count")
    public Integer count() {
        return userService.count();
    }

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @ResponseBody
    @RequestMapping("login")
    public Long login(String username, String password, HttpServletRequest request) {
        Long id = userService.login(username, password);

        // 设置登录状态
        if (id != null && id > 0l) {
            request.getSession().setAttribute("UID", id);
        }

        return id;
    }

    /**
     * 获取用户信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("userInfo")
    public Object fetchUserInfo(HttpServletRequest request) {

        Long id = (Long) request.getSession().getAttribute("UID");

        if (id == null) {
            // 未登录
            return -5l;
        }

        Long valid = userService.isValidUser(id);
        if (valid != 1) {
            return valid;
        }

        return userService.simpleUserInfo(id);
    }

    @ResponseBody
    @RequestMapping("userInfoByType")
    public Object fetchVerboseUserInfo(HttpServletRequest req) {
        Long id = (Long) req.getSession().getAttribute("UID");

        if (id == null) {
            return -5l;
        }

        Long valid = userService.isValidUser(id);
        if (valid != 1) {
            return valid;
        }

        User user = userService.simpleUserInfo(id);

        switch (user.getType()) {
            case 1:
                return user;
            case 2:
                return teacherService.findTeacherInfoById(id);
            case 3:
                return studentService.findStudentInfoById(id);
            default:
                return null;
        }
    }

    @ResponseBody
    @RequestMapping("modifyUserBySelf")
    public boolean modifySelfUser(@Param("username") String username, @Param("sex") Integer sex,
            @Param("birthday") String birthday, @Param("address") String address, @Param("note") String note,
            HttpServletRequest req) {
        Long id = (Long) req.getSession().getAttribute("UID");
        if (id == null) {
            return false;
        }

        boolean success = studentService.modifyStudentInfo(sex, birthday, address, username, note, id);

        if (!success) {
            return false;
        }
        return true;
    }

    @ResponseBody
    @RequestMapping("modifyPassword")
    public boolean modifyUserPasswordById(String password, HttpServletRequest req) {
        Long id = (Long) req.getSession().getAttribute("UID");

        if (id == null) {
            return false;
        }

        return userService.modifyUserPasswordById(password, id);
    }
}
